package com.axend.aerosense.room.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;

import androidx.annotation.Nullable;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.alibaba.android.arouter.launcher.ARouter;
import com.axend.aerosense.base.activity.MvvmBaseActivity;
import com.axend.aerosense.base.impl.ILoadDataResult;
import com.axend.aerosense.base.viewmodel.BaseViewModelFactroy;
import com.axend.aerosense.common.adapter.CommonListAdapter;
import com.axend.aerosense.common.adapter.ScreenAutoAdapter;
import com.axend.aerosense.room.R;
import com.axend.aerosense.room.databinding.RoomTimezonePickerActivityBinding;
import com.axend.aerosense.room.entity.PhoneAreaCodeBean;
import com.axend.aerosense.room.entity.PhoneCodeListBean;
import com.axend.aerosense.room.viewmodel.PhoneCodeViewModel;
import com.gyf.immersionbar.ImmersionBar;

import java.util.ArrayList;

public class PhoneCodePieckerActivity extends MvvmBaseActivity<RoomTimezonePickerActivityBinding, PhoneCodeViewModel>
        implements ILoadDataResult<PhoneCodeListBean> {

    public static int PERSON_REQUEST_CODE = 1214;
    public static int WARD_REQUEST_CODE = 1215;
    public static String PARAM_KEY = "phoneCode";
    private ArrayList<PhoneAreaCodeBean> phoneAreaCodeList = new ArrayList<>();
    private CommonListAdapter adapter;
    private PhoneAreaCodeBean selectedPhoneCode;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        ScreenAutoAdapter.match(this, 375.0f);
        super.onCreate(savedInstanceState);
        ARouter.getInstance().inject(this);
        ImmersionBar.with(this)
                .statusBarColor(android.R.color.white)
                .fitsSystemWindows(true)
                .statusBarDarkFont(true)
                .autoDarkModeEnable(true).init();
        setSupportActionBar(viewDataBinding.roomToolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);//左侧添加一个默认的返回图标
        getSupportActionBar().setHomeButtonEnabled(true); //设置返回键可用
        getSupportActionBar().setDisplayShowTitleEnabled(false);
        selectedPhoneCode = (PhoneAreaCodeBean) getIntent().getSerializableExtra(PARAM_KEY);
        init();
    }

    private void init() {
        viewDataBinding.roomTitle.setText(R.string.common_selecte_phone_code);
        adapter = new CommonListAdapter();
        viewDataBinding.roomList.setLayoutManager(new LinearLayoutManager(this));
        viewDataBinding.roomList.setAdapter(adapter);
        adapter.setList(phoneAreaCodeList);



        setLoadSir(viewDataBinding.roomList);
        showLoading();
        viewModel.onLoad();

        adapter.setOnItemClickListener((adapter, view, position) -> {
            ArrayList<PhoneAreaCodeBean> beans = (ArrayList<PhoneAreaCodeBean>) adapter.getData();
            selectedPhoneCode = beans.get(position);
            finish();
        });
    }

    @Override
    protected PhoneCodeViewModel getViewModel() {
        BaseViewModelFactroy<PhoneCodeListBean> myViewModelFactroy = new BaseViewModelFactroy<>(this, this);
        PhoneCodeViewModel tmp = new ViewModelProvider(this, myViewModelFactroy).get(PhoneCodeViewModel.class);
        return tmp;
    }

    @Override
    protected int getBindingVariable() {
        return 0;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.room_timezone_picker_activity;
    }

    @Override
    protected void onRetryBtnClick() {
        showLoading();
        viewModel.onLoad();
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            onBackPressed();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void finish() {
        Intent intent = new Intent();
        if(null != selectedPhoneCode){
            intent.putExtra(PARAM_KEY, selectedPhoneCode);
        }
        setResult(0 , intent);
        super.finish();
    }

    @Override
    public void loadDataSuccess(PhoneCodeListBean phoneCodeListBean) {
        phoneAreaCodeList.clear();
        phoneAreaCodeList.addAll(phoneCodeListBean.getPhoneAreaCodeInfo());
        adapter.setList(phoneAreaCodeList);
        adapter.notifyDataSetChanged();
    }

    @Override
    public void loadDataFail(String decs) {

    }
}
